﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using T_settlement.API.Application.Command.InvoiceCommand;
using T_settlement.API.Application.Command.OutputCommand;
using T_settlement.API.Application.Command.PaymentCommand;
using T_settlement.API.Application.Command.PayMoneyCommand;
using T_settlement.API.Application.Command.settlementCommand;
using T_settlement.API.Application.CommandHandlers.settlementCommandHandlers;

namespace T_settlement.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SettlementController : ControllerBase
    {
        private readonly IMediator mediator;

        public SettlementController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加收费结算信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> AddSettlement(AddsettlementCommand command)
        {
            try
            {
               return await mediator.Send(command);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除收费结算信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpDelete]
        public async Task<int> DelSettlement([FromQuery]DelSettlementCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 批量删除收费结算信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpDelete]
        public async Task<int> PDelSettlement([FromQuery]PDelSettlementCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 修改收费结算信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> UpdateSettlement(UpdatesettlementCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改付费审核状态
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> UpdateState(UpdateStateCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {
                throw;
            }
        }




        //---------------------------------------------------------------



        /// <summary>
        /// 添加付费结算信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> AddPayMoney(AddPayMoneyCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {

                throw;
            }
        }



        /// <summary>
        /// 删除付费结算信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpDelete]
        public async Task<int> DelPayMoney([FromQuery] DelPayMoneyCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {

                throw;
            }
        }



        /// <summary>
        /// 批量删除付费结算信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpDelete]
        public async Task<int> PDelPayMoney([FromQuery] PDelPayMoneyCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {
                throw;
            }
        }


        /// <summary>
        /// 修改付费结算信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> UpdatePay(UpdatePayMoneyCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改收费审核状态
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> UpdatePayState(UpdatePayStateCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {
                throw;
            }
        }


        //----------------------------------------------------------------------------

        /// <summary>
        /// 添加进项发票管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> AddInvoice(AddInvoiceCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 删除进项发票管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpDelete]
        public async Task<int> DelInvoice([FromQuery]DelInvoiceCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {
                throw;
            }
        }


        /// <summary>
        /// 批量删除进项发票管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpDelete]
        public async Task<int> PDelInvoice([FromQuery]PDelInvoiceCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 修改进项发票管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> UpdateInvoice(UpdateInvoiceCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {

                throw;
            }
        }


        //--------------------------------------------------------------------

        /// <summary>
        /// 添加销项发票管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> AddOutput(AddOutputCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {
                throw;
            }
        }


        /// <summary>
        /// 删除销项发票管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpDelete]
        public async Task<int> DelOutput([FromQuery] DelOutputCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 批量删除销项发票管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpDelete]
        public async Task<int> PDelOutput([FromQuery] PDelOutputCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {
                throw;
            }
        }


        /// <summary>
        /// 修改销项发票管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> UpdateOutput( UpdateOutputCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {
                throw;
            }
        }

         //-----------------------------------------------------------

        /// <summary>
        /// 添加付款信息（提交审批）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> AddPayment(AddPaymentCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 添加付款信息（保存草稿）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> AddsPayment(AddsPaymentCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 删除付款信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpDelete]
        public async Task<int> DelPayment([FromQuery]DelPaymentCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 修改付款信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> UpdatePayment(UpdatePaymentCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {
                throw;
            }
        }


        /// <summary>
        /// 批量删除付款信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpDelete]
        public async Task<int> PDelPayment([FromQuery]PDelPaymentCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        /// 
        [HttpPost]

        public async Task<string>FilUpload(IFormFile file)
        {
            try
            {
                string path =Directory.GetCurrentDirectory()+"/wwwroot/Files/"+file.FileName;
                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    await file.CopyToAsync(fs);
                    fs.Flush();
                }
                return "http://localhost:20426/Files/"+file.FileName;
            }
            catch (Exception)
            {
                throw;
            }
        }


        /// <summary>
        /// 修改付款信息(审计状态)
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> StatePayment(StatePaymentCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 修改付款信息(审计状态)(接口)
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> StateSPayment(StateSPaymentCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception)
            {
                throw;
            }
        }

    }
}
